import {
    querCurrencyRateList, addCurrencyRate, deleteCurrencyRate, getCurrencyRateById, updateCurrencyRate,
    uploadCurrencyRate, synchronizeCurrencyRate
} from '../../services/basedata/exchangeRate';
import { parse } from 'qs';
import { message } from 'antd';

export default {
    namespace: 'exchangeRate',
    state: {
        search: { // 查询条件
            currencyCode: '',
            exchangeRate: '',
            status: '1'
        },
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        editModel: { // 编辑视图
            showStatus: false, // 默认不显示
            confirmLoading: false, // 确认按钮loading状态
            title: '创建汇率', // 标题
            type: 'add',
            form: {
                code: '',
                name: '',
                targetcurrency: '',
                rate: '',
                status: '1'
            }
        },
        upload: {
            showStatus: false,
            confirmLoading: true,
            title: "上传汇率",
            type: "add",
            form: {
                url: '',
            }
        }
    },
    reducers: {
        /**
         * 设置查询条件
         *
         * @param state
         * @param actio
         * @returns {{search}}
         */
        setSearch(state, action) {
            return { ...state, search: action.payload.search };
        },

        /**
         * 是否显示
         *
         * @param state
         * @param action
         * @returns {{showStatus: (boolean|*)}}
         */
        setEditModel(state, action) {
            let payload = action.payload;

            let newState = { ...state.editModel };
            if (payload.showStatus != undefined) {
                newState.showStatus = payload.showStatus;
            }
            if (payload.title != undefined) {
                newState.title = payload.title;
            }
            if (payload.confirmLoading != undefined) {
                newState.confirmLoading = payload.confirmLoading;
            }
            if (payload.type != undefined) {
                newState.type = payload.type;
            }
            if (payload.form != undefined) {
                newState.form = payload.form;
            }

            return { ...state, editModel: { ...newState } };
        },


        setUpload(state, action) {

            let payload = action.payload;

            let newState = { ...state.upload };
            if (payload.showStatus != undefined) {
                newState.showStatus = payload.showStatus;
            }
            if (payload.title != undefined) {
                newState.title = payload.title;
            }
            if (payload.confirmLoading != undefined) {
                newState.confirmLoading = payload.confirmLoading;
            }
            return { ...state, upload: { ...newState } }

        },
        /**
         * 设置数据列表
         *
         * @param state
         * @param action
         */
        setList(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page }
        },

        /**
         * 设置页码
         *
         * @param state
         * @param payload
         * @returns {{page: *}}
         */
        setPage(state, { payload }) {
            return { ...state, page: payload.page }
        }
    },
    effects: {

        /**
         * 查询汇率集合
         *
         * @param call
         * @param put
         * @param select
         */
        *querCurrencyRateList({ }, { call, put, select }) {

            const search = yield select(state => state.exchangeRate.search);
            const page = yield select(state => state.exchangeRate.page);
            const res = yield call(querCurrencyRateList, { code: search.code, name: search.name, status: search.status, beginTime: search.beginTime, endTime: search.endTime, page });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                yield put({
                    type: 'setList',
                    payload: {
                        list: content.list,
                        page: content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 查询汇率
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getCurrencyRateById({ payload }, { call, put, select }) {
            const res = yield call(getCurrencyRateById, { id: payload.id });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: true,
                        confirmLoading: false,
                        title: "修改汇率",
                        type: 'update',
                        form: {
                            id: content.id,
                            code: content.code,
                            name: content.name,
                            targetcurrency: content.targetcurrency,
                            rate: content.rate,
                            ratetime: content.ratetime,
                            status: content.status
                        }
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 修改汇率
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *updateCurrencyRate({ payload }, { call, put, select }) {
            const res = yield call(updateCurrencyRate, payload);
            if (res.data.resultCode === "ok") {
                message.info("修改成功！重新登录后生效");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'querCurrencyRateList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 删除汇率
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *deleteCurrencyRate({ payload }, { call, put, select }) {
            const res = yield call(deleteCurrencyRate, { id: payload.id });
            if (res.data.resultCode === "ok") {
                message.info("删除成功！");

                yield put({
                    type: "querCurrencyRateList"
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 增加汇率
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addCurrencyRate({ payload }, { call, put, select }) {
            const res = yield call(addCurrencyRate, payload);
            if (res.data.resultCode === "ok") {
                message.info("增加成功！");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'querCurrencyRateList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        *uploadRate({ payload }, { call, put, select }) {
            let file = payload.file;
            const res = yield call(uploadCurrencyRate, {
                fileUrl: file
            });
            if (res.data.resultCode == "ok") {
                yield put({
                    type: "setUpload",
                    payload: {
                        showStatus: false,
                        confirmLoading: false,
                    }
                })
                message.info("汇率增加成功");
            } else {
                message.error(res.data.errMessage);
            }
        },

        *showUploadRate({ payload }, { call, put, select }) {
            yield put({
                type: 'setUpload',
                payload: {
                }
            });
        },

        *synchronizeCurrencyRate({ payload }, { call }) {
            const res = yield call(synchronizeCurrencyRate);
            if (res.data.resultCode === "ok") {
                message.info("外管局汇率同步成功!");
            } else {
                message.error(res.data.errMessage);
            }
        },


    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                if (pathname.includes('basedata/exchangeRate')) {
                    dispatch({
                        type: 'querCurrencyRateList'
                    });
                }

            });
        }
    }
};
